//
//  BGRegisterViewController.swift
//  Baga
//
//  Created by 飞王 on 16/12/30.
//  Copyright © 2016年 飞王. All rights reserved.
//

import UIKit

class BGRegisterViewController: UIViewController,UITextFieldDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white;
        
        setupNavView();

        // Do any additional setup after loading the view.
        setupViews();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func setupNavView() {
        title = "欢迎来到巴格";
    }
    

    private func setupViews() {
        
//        view.addSubview(welcomeLabel);
        view.addSubview(phoneTextFeild);
        view.addSubview(verificationTextFeild);
        view.addSubview(getVerificationButton);
        view.addSubview(passwordTextFeild);
        view.addSubview(registerButton);
        view.addSubview(userProtocolButton);
        
        
        makeConstraints();
    }
    
    private func makeConstraints() {
//        welcomeLabel.snp.makeConstraints { (make) in
//            make.top.equalTo(view.snp.top).offset(84);
//            make.centerX.equalTo(view).offset(0);
//            make.left.equalTo(view);
//            make.right.equalTo(view);
//            make.height.equalTo(welcomeLabel.snp.width).multipliedBy(0.2);
//        }
        
        phoneTextFeild.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.top).offset(84);
            make.centerX.equalTo(view);
            make.left.equalTo(view).offset(marginH);
            make.right.equalTo(view).offset(-marginH);
            make.height.equalTo(phoneTextFeild.snp.width).multipliedBy(0.2);
            
        }
        
        verificationTextFeild.snp.makeConstraints { (make) in
            make.top.equalTo(phoneTextFeild.snp.bottom).offset(marginV);
            make.centerX.equalTo(view);
            make.left.equalTo(view).offset(marginH);
            make.right.equalTo(view).offset(-marginH);
            make.height.equalTo(verificationTextFeild.snp.width).multipliedBy(0.2);
        }
        
        getVerificationButton.snp.makeConstraints { (make) in
            make.top.equalTo(phoneTextFeild.snp.bottom).offset(marginV);
            make.right.equalTo(verificationTextFeild).offset(0);
            make.width.equalTo(120);
            make.height.equalTo(verificationTextFeild.snp.height).offset(-11);
        }
        
        passwordTextFeild.snp.makeConstraints { (make) in
            make.top.equalTo(verificationTextFeild.snp.bottom).offset(marginV);
            make.centerX.equalTo(view);
            make.left.equalTo(view).offset(marginH);
            make.right.equalTo(view).offset(-marginH);
            make.height.equalTo(passwordTextFeild.snp.width).multipliedBy(0.2);
        }
        
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(passwordTextFeild.snp.bottom).offset(marginV);
            make.centerX.equalTo(view);
            make.left.equalTo(view).offset(marginH);
            make.right.equalTo(view).offset(-marginH);
            make.height.equalTo(passwordTextFeild.snp.width).multipliedBy(0.2);
        }
        
        userProtocolButton.snp.makeConstraints { (make) in
            make.top.equalTo(registerButton.snp.bottom).offset(marginV);
            make.centerX.equalTo(view);
            make.width.equalTo(180);
            make.height.equalTo(30);
        }
        
        
        
    }
    
    //MARK: - 业务逻辑
    
    //获取验证码
    @objc private func getVerificationCode () {
        if phoneTextFeild.text != nil {
            
            let  parameters: Dictionary = [
                "phone":phoneTextFeild.text!,
                ];
            
            BGNetworkTools.sharedTools.request(method:.POST, urlString: getVerificationMessageUrl, parameters: parameters as AnyObject?) { (respose, error) in
                if error == nil  {
                    
                    
                    printLog(message: "get verification message success!")
                }else {
                    self.navigationController?.view.makeToast("获取验证码失败")
                    printLog(message: "get verification message fail!")
                }
                
            };
        }
        
        
    }
    
    //注册
    @objc private func userReginster() {
        let loginParameters:Dictionary = [
            "user_phone":phoneTextFeild.text!,
            "user_pwd":passwordTextFeild.text!
        ]
        
        BGNetworkTools.sharedTools.request(urlString: userRegisterUrl, parameters: loginParameters as AnyObject?) { (respose, error) in
            
            if (error != nil) {
                printLog(message: error);
            } else {
                // do register succesee
                self.navigationController?.popViewController(animated: true);
                
            }
            
        };
    }
    
    //用户协议
    
    @objc private func goUserPotrocalViewController() {
        let userPotrocalController = BGUserPotrocalController();
        self.navigationController?.pushViewController(userPotrocalController, animated: true);
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        printLog(message: "需要收起键盘吗？");
        if phoneTextFeild.isFirstResponder {
            phoneTextFeild.resignFirstResponder();
        }
        if verificationTextFeild.isFirstResponder {
            verificationTextFeild.resignFirstResponder();
        }
        if passwordTextFeild.isFirstResponder {
            passwordTextFeild.resignFirstResponder();
        }
    }
    
    // MARK: - LazyLoad
    private lazy var welcomeLabel: UILabel = {
        let welcomeLabel = UILabel();
        welcomeLabel.text = "欢迎来到巴格";
        welcomeLabel.textColor = UIColor.orange;
        welcomeLabel.font = UIFont(name: "", size: 20);
        welcomeLabel.textAlignment = .center;
        
        return welcomeLabel;
    }()
    
    private lazy var phoneTextFeild: BGUserInforTextField = {
        let phoneTextFeild = BGUserInforTextField(placeholderText: "手机号码", leftImageName: "zhuce_icon_shouji");
        phoneTextFeild.keyboardType = UIKeyboardType.numberPad;
        phoneTextFeild.delegate = self;
        return phoneTextFeild;
        
    }()
    
    private lazy var verificationTextFeild: BGUserInforTextField = {
        let verificationTextFeild = BGUserInforTextField(placeholderText: "验证码", leftImageName: "zhuce_icon_yanzheng");
        return verificationTextFeild;
        
    }()
    
    private lazy var passwordTextFeild: BGUserInforTextField = {
        let passwordTextFeild = BGUserInforTextField(placeholderText: "请输入密码", leftImageName: "zhuce_icon_lock");
        return passwordTextFeild;
        
    }()
    
    private lazy var getVerificationButton: UIButton = {
        let getVerificationButton = UIButton();
        getVerificationButton.backgroundColor = UIColor.lightGray;
        getVerificationButton.setTitle("获取验证码", for: .normal);
        getVerificationButton.isEnabled = false;
        getVerificationButton.addTarget(self, action: #selector(getVerificationCode), for: .touchUpInside);
        
        return getVerificationButton;
    }()
    
    
    private lazy var registerButton: UIButton = {
        let registerButton = UIButton();
        registerButton.setTitle("注册", for: .normal);
        registerButton.tintColor = UIColor.white;
        registerButton.addTarget(self, action: #selector(userReginster), for: .touchUpInside);
        registerButton.backgroundColor = UIColor.orange;
        
        return registerButton;
    }()
    
    private lazy var userProtocolButton: UIButton = {
        let userProtocolButton = UIButton();
        userProtocolButton.setTitle("《巴格用户注册协议》", for: .normal);
        userProtocolButton.titleLabel?.font = UIFont(name: "", size: 9);
        userProtocolButton.setTitleColor(UIColor.lightGray, for: .normal);
        userProtocolButton.addTarget(self, action: #selector(goUserPotrocalViewController), for: .touchUpInside);
        
        return userProtocolButton;
    }()

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == phoneTextFeild {
            let text:NSMutableString = textField.text!.mutableCopy() as! NSMutableString
            text.replaceCharacters(in: range, with: string)
            
            return text.length <= 11
        }
        return (textField.text != nil);
    }
}


